# -*- coding: utf-8 -*-
from model import Collection, db
from time import time


class Answer(Collection):
    '''
* `_id` (ObjectId) - ID of answer
* `content` ([dict]) - content of answer
    * `aType` (int) - 2 表示是画室的回复，1 表示是老师的回复, 0 是题主的回复
    * `content` (string) - content
* `createTime` (long) - 回答被创建的时间
* `problemId` (string) - ID of problem the answer belongs
* `updateTime` (long) - 回答最后一次被更新的时间
* `userId` (string) - teacher ID of the answer
    '''

    collection = db.answer

    class Field(object):
        _id = '_id'
        content = 'content'
        createTime = 'createTime'
        problemId = 'problemId'
        updateTime = 'updateTime'
        userId = 'userId'

    class AType(object):
        studio = 2
        teacher = 1
        student = 0

    class Content(object):
        aType = 'aType'
        content = 'content'

    @staticmethod
    def new_answer(user_id, problem_id, content, aType=1):
        return Answer.insert({
            Answer.Field.content: [{
                Answer.Content.aType: aType,
                Answer.Content.content: content
            }],
            Answer.Field.createTime: int(time() * 1000),
            Answer.Field.problemId: problem_id,
            Answer.Field.updateTime: int(time() * 1000),
            Answer.Field.userId: user_id
        })

    @staticmethod
    def update_answer(answer_id, content, a_type=1):
        Answer.collection.update({
            Answer.Field._id: answer_id,
        }, {
            '$push': {
                Answer.Field.content: {
                    Answer.Content.aType: a_type,
                    Answer.Content.content: content
                }
            },
            '$set': {
                Answer.Field.updateTime: int(time() * 1000)
            }
        })
